/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uva;

import java.util.ArrayList;
import java.util.Collections;

/**
 *
 * @author mohamed.azouz
 */
public class Permutation {
    public static void permutation(String str) {
        ArrayList<String> list = new ArrayList<String>();
        permutation("", str, list);
        Collections.sort(list);
        for (String s : list) {
            System.out.println(s);
        }
    }

    private static void permutation(String prefix, String str, ArrayList<String> list) {
        int n = str.length();
        if (n == 0) {
            list.add(prefix);
        } else {
            for (int i = 0; i < n; i++) {
                permutation(prefix + str.charAt(i), str.substring(0, i) + str.substring(i + 1, n), list);
            }
        }
    }
}
